package com.example.leetdemo.process;

import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;

public class Solution1606 {

    int time = 0;

    public List<Integer> busiestServers(int k, int[] arrival, int[] load) {
        TreeSet<Integer> free = new TreeSet<>();
        for (int i = 0; i < k; i ++) {
            free.add(i);
        }
        PriorityQueue<int[]> busy = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));

        for (int i = 0; i < arrival.length; i++) {
            while (!busy.isEmpty() && busy.peek()[1] < arrival[i]) {
                free.add(busy.poll()[0]);
            }
            if (!free.isEmpty()) {

                Integer p = free.ceiling(i % k);
                if (p == null) {
                    p = free.first();
                }
                int [] work = new int[2];
                work[0] = free.pollFirst();
                work[1] = arrival[i] + load[i];
                busy.add(work);
            }
        }
        return null;
    }
}
